package com.bobbygu.server.simpleapp;

import com.bobbygu.server.simpleapp.controller.GetIpAddrController;
import com.bobbygu.server.simpleapp.socket.ConnectionServer;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

@SpringBootApplication
@MapperScan("com.bobbygu.server.simpleapp.mapper")// mybatis扫描路径
public class SimpleappApplication {

    public static void main(String[] args) {
        getLocalIpAddr();
        ConnectionServer.run();
        SpringApplication.run(SimpleappApplication.class, args);
    }

    /**
     * 获取本地ip地址
     */
    private static void getLocalIpAddr() {
        Enumeration allNetInterfaces = null;
        try {
            allNetInterfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        InetAddress ip;
        while (allNetInterfaces != null && allNetInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
//            System.out.println(netInterface.getName());
            Enumeration addresses = netInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                ip = (InetAddress) addresses.nextElement();
                if (ip != null && ip instanceof Inet4Address) {
                    GetIpAddrController.ipAddr += "本机的IP = " + ip.getHostAddress();
                    System.out.println("本机的IP = " + ip.getHostAddress() + "\n");
                }
            }
        }
    }
}
